package com.liuyk.commonlib.fragment;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.dialog.OtherDialogUtils;
import com.liuyk.commonlib.dialog.ToastUtils;

/**
 * 基类fragment
 * <p>
 * Created by liuyk on 2019/10/16.
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    private ViewGroup mParentContain;
    private ViewGroup mContainer;
    private LinearLayout mTitleBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.base_main_fragment_layout, container, false);
        mParentContain = view.findViewById(R.id.parent_contain);
        mTitleBar = view.findViewById(R.id.fragment_title_bar_contain);
        mContainer = view.findViewById(R.id.container);
        mContainer.addView(getContentView(inflater, mContainer), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        createView();
        return view;
    }

    protected abstract void createView();

    protected <T extends View> T findViewById(int resId) {
        return mParentContain.findViewById(resId);
    }

    protected void setTitleBarView(int resId) {
        View titleView = LayoutInflater.from(getContext()).inflate(resId, mTitleBar, false);
        setTitleBarView(titleView);
    }

    protected void setTitleBarView(View view) {
        mTitleBar.addView(view);
    }

    public void hideTitleBar() {
        mTitleBar.setVisibility(View.GONE);
    }

    public void showTitleBar() {
        mTitleBar.setVisibility(View.VISIBLE);
    }

    protected View inflateView(int resId) {
        return LayoutInflater.from(getContext()).inflate(resId, mParentContain, false);
    }

    protected void showToast(String msg) {
        ToastUtils.showToast(getContext(), msg);
    }

    protected void showToast(int resId) {
        showToast(getString(resId));
    }

    protected void showProgress(String message) {
        OtherDialogUtils.showLoading(getActivity(), message);
    }

    protected void dismissProgress() {
        OtherDialogUtils.dismissLoading();
    }

    protected abstract View getContentView(LayoutInflater inflater, ViewGroup container);

    public void setBaseBackground(int resId) {
        mContainer.setBackgroundResource(resId);
    }

    public void setBaseBackgroundColor(int resId) {
        mContainer.setBackgroundColor(resId);
    }

    protected PopupWindow createPopup(View view) {
        return createPopup(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    protected PopupWindow createPopup(View view, int width, int height) {
        final PopupWindow popupWindow = new PopupWindow(view, width, height);
        popupWindow.setFocusable(true);//设置焦点
        popupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景位图
        popupWindow.setAnimationStyle(R.style.popup_animation);//设置动画
        return popupWindow;
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    protected void windowToAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getActivity().getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        //在BaseActivity中实现onClick，其子类不必在实现，但不可重写，否则父类的点击事件就没了
        //子类务必要写super.onClick();
    }
}
